package view;

import java.util.Iterator;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import model.Fk011VORowImpl;

import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.data.RichTable;

import oracle.binding.BindingContainer;
import oracle.binding.OperationBinding;

import oracle.jbo.Key;
import oracle.jbo.Row;
import oracle.jbo.RowSetIterator;
import oracle.jbo.uicli.binding.JUCtrlHierBinding;
import oracle.jbo.uicli.binding.JUCtrlHierNodeBinding;

import org.apache.myfaces.trinidad.event.SelectionEvent;
import org.apache.myfaces.trinidad.model.CollectionModel;
import org.apache.myfaces.trinidad.model.RowKeySet;

public class Fk011MB {

    private RichTable tablica;

    public Fk011MB() {
    }


    public void b4_action() {
        // Add event code here...
        BindingContainer bindings = BindingContext.getCurrent().getCurrentBindingsEntry();
        OperationBinding operationBinding = (OperationBinding) bindings.getOperationBinding("viewObjectOutput");
        operationBinding.execute();
    }

    public void setTablica(RichTable tablica) {
        this.tablica = tablica;
    }

    public RichTable getTablica() {
        return tablica;
    }

    public String b4_action2() {
        DCBindingContainer bindings = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding iterator = bindings.findIteratorBinding("Fk011VOIterator");
        Row[] rows = iterator.getAllRowsInRange();
        for (Row row : rows) {
            Fk011VORowImpl red = (Fk011VORowImpl) row;
            System.out.println(red.getK11naz());
        }
        return null;
    }

    public void t1_selectionListener(SelectionEvent selectionEvent) {
        RichTable richTable = (RichTable) selectionEvent.getSource();
        CollectionModel tableModel = (CollectionModel) tablica.getValue();
        JUCtrlHierBinding adfTableBinding = (JUCtrlHierBinding) tableModel.getWrappedData();
        // System.out.println(tablica.getActiveRowKey());
        Object selectedRowData = tablica.getSelectedRowData();
        //System.out.println(tablica.getRowIndex());
        JUCtrlHierNodeBinding nodeBinding = (JUCtrlHierNodeBinding) selectedRowData;
        System.out.println(nodeBinding.getAttribute("K11naz"));
        for (Object o : nodeBinding.getAttributeValues()) {
            // System.out.println("Selected values " + o);
            // Add event code here...


        }

        /*   DCBindingContainer bindings =
               (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
           DCIteratorBinding dcItteratorBindings =
               bindings.findIteratorBinding("Fk011VOIterator");

           // Get an object representing the table and what may be selected within it
           ViewObject voTableData = dcItteratorBindings.getViewObject();

           // Get selected row
           Row rowSelected = voTableData.getCurrentRow();
           System.out.println(rowSelected.getAttribute("K11naz"));*/
    }


    public String b6_action() {
        // Add event code here...
        DCBindingContainer bindings = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding iterator = bindings.findIteratorBinding("Fk011VOIterator");
        Row red = iterator.getCurrentRow();
        System.out.println(red.getAttribute("K11naz"));
        return null;
    }

    public String poruka() {
        DCBindingContainer dcb = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding iterator = dcb.findIteratorBinding("Fk011VOIterator");
        Fk011VORowImpl red = (Fk011VORowImpl) iterator.getCurrentRow();
        FacesMessage fm = new FacesMessage(red.getK11naz());
        fm.setSeverity(FacesMessage.SEVERITY_INFO);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, fm);
        return null;
    }


    public String b7_action() {
        // Add event code here...
        DCBindingContainer bindings = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding iterator = bindings.findIteratorBinding("Fk011VOIterator");
        Fk011VORowImpl red = (Fk011VORowImpl) iterator.getRowAtRangeIndex(1);
        System.out.println(iterator.getRowKeyString(red));
        iterator.setCurrentRowWithKey(iterator.getRowKeyString(red));
        return null;
    }

    public String b3_action() {
        /*
         * Da bi ovo radilo, na tablica treba ocistiti property-e SelectedRowKeys i SelectionListener, u suprotnom
         * uvijek prikazuje samo zadnji odabrani red
         */
        RowKeySet odabrano = tablica.getSelectedRowKeys();        
        Iterator iterator = odabrano.iterator();
        DCBindingContainer bindings = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
        DCIteratorBinding iter = bindings.findIteratorBinding("Fk011VOIterator");
        RowSetIterator redIterator = iter.getRowSetIterator();
        while (iterator.hasNext()) {
            Key kljuc = (Key) ((List) iterator.next()).get(0);
            Fk011VORowImpl red = (Fk011VORowImpl) redIterator.getRow(kljuc);
            System.out.println(kljuc);
            System.out.println(red.getK11naz());
        }
        return null;

    }
}
